package com.zeng.vueserve.coreservice.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.vueserve.coreservice.mapper.UserMapper;
import com.zeng.vueserve.coreservice.entity.User;
import com.zeng.vueserve.coreapi.vo.UserVo;
import com.zeng.vueserve.coreapi.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserVo addOne(UserVo userVo) {
    User user = User.builder().build();
    BeanUtils.copyProperties(userVo, user, IgnoreStringUtil.ignoreExclude());
    int insert = userMapper.insert(user);
    return BeanUtil.beanToVo(user, userVo);
    }

    @Override
    public UserVo getOneByCode(String code) {

    User user = this.userMapper.selectById(code);
    UserVo userVo = BeanUtil.beanToVo(user, new UserVo());

    return userVo;
    }

    @Override
    public Integer update(UserVo userVo) {
    User user = BeanUtil.VoToBean(userVo,
    new User(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.userMapper.updateById(user);
    }

    @Override
    public Integer delete(String code) {
    return this.userMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.userMapper.deleteBatchIds(codes);
    }

    @Override
    public List<UserVo> findPage(Page page, UserVo userVo) {
        User user = BeanUtil.VoToBean(userVo, new User());
        this.userMapper.selectPage(page, this.getWrapper(userVo));
        List orders = page.getRecords();
        List<UserVo> userVos = new ArrayList<>();
        orders.forEach(e->{
            userVos.add(BeanUtil.beanToVo(e, new UserVo()));
        });
        page.setRecords(null);
        return userVos;
        }


    @Override
    public Integer count(UserVo userVo) {
        Integer integer = this.userMapper.selectCount(this.getWrapper(userVo));
        return integer;
    }

    public AbstractWrapper getWrapper(UserVo userVo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (userVo != null) {


        }
        return wrapper;
    }
}
